package com.glodon.paas.document.api.bean;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.glodon.paas.account.api.bean.User;

public class File {

    private String              id;
    private String              parentId;
    private String              projectId = "";
    private String              name;
    private String              extension;
    private String              ownerId;
    private String              updator;
    private String              creator;
    private Date                createTime;
    private Date                updateTime;
    private Integer             count;
    private Long                size;
    private boolean             folder;
    private boolean             shared;
    private boolean             published;
    private Lock                lock;
    private int                 folderCount;
    private boolean             hasFolder;
    private List<File>          files;
    private List<Revision>      revisions;
    private Map<String, Object> attributes;
    private User                owner;
    private boolean             deleted;
    private boolean             autoDeleted;
    private Integer             revisionNumber;
    private String              dataBagId;
    private String              description;

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }
    
    public boolean isAutoDeleted() {
        return autoDeleted;
    }

    public void setAutoDeleted(boolean autoDeleted) {
        this.autoDeleted = autoDeleted;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, Object> getAttributes() {
        if(this.attributes == null){
            this.attributes = new HashMap<String, Object>();
        }
        return this.attributes;
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    /**
     * get the latest revision of this document
     * 
     * @return
     */
    public Revision latestRevision() {
        Revision latest = null;
        if (revisions != null) {
            for (Revision item : revisions) {
                if (latest == null || latest.getRevisionNumber() < item.getRevisionNumber()) {
                    latest = item;
                }
            }
        }
        return latest;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    /**
     * get the latest available revision of this document
     * 
     * @return
     */
//    public Revision latestAvailableRevision() {
//        Revision latest = null;
//        if (revisions != null) {
//            for (Revision item : revisions) {
//                if (StringUtil.isNotEmpty(item.getStorageKey())
//                    && (latest == null || latest.getRevisionNumber() < item.getRevisionNumber())) {
//                    latest = item;
//                }
//            }
//        }
//        return latest;
//    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    /**
     * get the latest Unavailable revision of this document
     * 
     * @return
     */
//    public Revision latestUnavailableRevision() {
//        Revision latest = null;
//        if (revisions != null) {
//            for (Revision item : revisions) {
//                if (StringUtil.isEmpty(item.getStorageKey())
//                    && (latest == null || latest.getRevisionNumber() < item.getRevisionNumber())) {
//                    latest = item;
//                }
//            }
//        }
//        return latest;
//    }

//    /**
//     * get the specified revision of this document
//     * 
//     * @param versionNumber
//     * @return
//     */
//    public Revision getRevision(int revisionNumber) {
//        for (Revision item : revisions) {
//            if (item.getRevisionNumber() == revisionNumber) {
//                return item;
//            }
//        }
//        return null;
//    }

    /**
     * append a new version to this document
     * 
     * @param item
     */
    public void appendVersion(Revision item) {
        item.setRevisionNumber(nextRevisionNumber());
        revisions.add(item);
    }

    private int nextRevisionNumber() {
        return revisions.size() + 1;
    }

    public void setFolder(boolean folder) {
        this.folder = folder;
    }

    public boolean isFolder() {
        return folder;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public String getExtension() {
        return extension;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public List<Revision> getRevisions() {
        return revisions;
    }

    public void setRevisions(List<Revision> revisions) {
        this.revisions = revisions;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    // public boolean isShared() {
    // return shareType == DocumentConstant.SHARE_TYPE_SHARED || shareType ==
    // DocumentConstant.SHARE_TYPE_SHARED_AND_PUBLISHED;
    // }
    //
    // public boolean isPublished() {
    // return shareType == DocumentConstant.SHARE_TYPE_PUBLISHED || shareType ==
    // DocumentConstant.SHARE_TYPE_SHARED_AND_PUBLISHED;
    // }

    @JsonIgnore
    public String getFullName() {
        if (this.extension != null && !"".equals(this.extension)) {
            return this.name + "." + this.extension;
        } else {
            return this.name;
        }
    }

    public Integer getRevisionNumber() {
        return revisionNumber;
    }

    public void setRevisionNumber(Integer revisionNumber) {
        this.revisionNumber = revisionNumber;
    }

    public String getDataBagId() {
        return dataBagId;
    }

    public void setDataBagId(String dataBagId) {
        this.dataBagId = dataBagId;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Lock getLock() {
        return lock;
    }

    public void setLock(Lock lock) {
        this.lock = lock;
    }

    public String getUpdator() {
        return updator;
    }

    public void setUpdator(String updator) {
        this.updator = updator;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public boolean isShared() {
        return shared;
    }

    public void setShared(boolean shared) {
        this.shared = shared;
    }

    public boolean isPublished() {
        return published;
    }

    public void setPublished(boolean published) {
        this.published = published;
    }

    public int getFolderCount() {
        return folderCount;
    }

    public void setFolderCount(int folderCount) {
        this.folderCount = folderCount;
        this.setHasFolder(folderCount > 0);
    }

    public boolean isHasFolder() {
        return hasFolder;
    }

    public void setHasFolder(boolean hasFolder) {
        this.hasFolder = hasFolder;
    }
    
    public List<File> getFiles() {
        return files;
    }
    
    public void setFiles(List<File> files) {
        this.files = files;
    }

    
    public String getDescription() {
        return description;
    }

    
    public void setDescription(String description) {
        this.description = description;
    }
}
